dofile (getScriptPath() .. "\\..\\processor\\processor.lua")

if not processor.ds then


dofile (getScriptPath() .. "\\..\\processor\\log.lua")
dofile (getScriptPath() .. "\\..\\lib\\lib.lua")


processor.ds = {}
processor.ds.DataSources = {}
processor.ds.Callback = [[
	if not processor.ds.started then return end
	local index = ...
	if not index or index < 1 then return end
	local v = processor.ds.DataSources[%d]["%s"]
	--if v.sec_code == "FIVE" then
		--message(index .. " of " .. v.ds:Size())
	--end
	if v and v.ds and index == v.ds:Size() then
	--if v and v.ds and v.ds:Size() - index < 2 then
		processor.ds:Push(v)
	end
]]


function processor.ds:Init(fn, interval_param, func, thread)
	if self.DataSources[interval] then return end
	
	local list = lib:LoadQuotes(fn, thread, self.threads_number)
	if not list or #list <= 0 then return end
	
	local interval = interval_param
	if not interval then interval = INTERVAL_M1 end

	if self.threads_number then
		self.thread = thread
	end
	
	self.DataSources[interval] = {}

	local callback, err, instrument
	for i = 1, #list do
		if not self.DataSources[interval][list[i].sec_code] then
			instrument = lib:GetInstrument(list[i].sec_code)
			if instrument then
				callback, err = load(
					string.format(self.Callback,
						interval,
						list[i].sec_code
					)
				)
				if callback then
					self.DataSources[interval][list[i].sec_code] = {
						class_code = instrument.class_code,
						sec_code = list[i].sec_code,
						interval = interval,
						turnover = list[i].value,
						callback = callback,
						func = func
					}
					processor.ds.started = nil
				else
					processor:log("loadstring error " .. list[i].sec_code .. " " .. interval .. " " .. err)
				end
			else
				processor:log("GetInstrument error " .. list[i].sec_code)
			end
		end
	end
	return self.DataSources[interval]
end


function processor.ds:GetSecuritiesList(interval)
	if self.DataSources[interval] then
		local info
		local list = {}
		for k, v in pairs(self.DataSources[interval]) do
			info = lib:GetInstrument(v.sec_code)
			table.insert(list, info)
		end
		return list
	end
end


function processor.ds:GetTime(v)
	if not v or not v.ds or int(v.ds:Size()) < 1 then return end
	local t = v.ds:T(v.ds:Size())
	if not t then return end
	if v.interval == INTERVAL_TICK
		and lib:tickseconds() - lib:tickseconds(t) > 2
	then return end
	return lib:ticktime(t)
end


function processor.ds:Push(v)
	local t = self:GetTime(v)
	if t and (v.interval == INTERVAL_TICK or v.time ~= t) then
		v.time = t
		if v.func then
			v.func(v)
		end
		processor:PushData("DataSource", v)
	end
end


processor:AddCallback("DataSource")


processor:AddTimer(function()
	if not processor.ds.started then
		local ds, err, no_ds
		local n = 0
		local turnover = 0
		for _, vi in pairs(processor.ds.DataSources) do
			for _, v in pairs(vi) do
				if v.ds then
					n = n + 1
					turnover = turnover + v.turnover
				elseif processor.ds.started then
					processor.ds.started = nil
					return
				else
					ds, err = lib:CreateDataSource(v.sec_code, v.interval)
					if ds then
						if ds:SetUpdateCallback(v.callback) then
							v.ds = ds
						else
							processor:log("SetUpdateCallback error", v.sec_code, v.interval)
						end
						if v.ds then
							processor:log("DataSource created " .. v.sec_code .. " " .. v.interval)
						else
							ds:Close()
						end
					else
						processor:log("CreateDataSource error", err, v.sec_code, v.interval)
					end
					if v.ds then
						no_ds = true
						break
					end
				end
			end
		end
		if not no_ds then
			processor.ds.started = n
			if n > 0 then
				if processor.ds.thread then
					message("DataSources [" .. processor.ds.thread .. "] Started " .. n .. " " .. turnover)
				else
					message("DataSources Started " .. n .. " " .. turnover)
				end
			end
		end
	end
end)


processor:AddStop(function()
	local n = 0
	for ki, vi in pairs(processor.ds.DataSources) do
		for k, v in pairs(vi) do
			if v.ds then
				v.ds:SetEmptyCallback()
				v.ds:Close()
				n = n + 1
			end
		end
	end
	processor.ds.DataSources = {}
	if n > 0 then
		message("DataSources Closed " .. n)
	end
end)


end -- of file
